/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.assessment.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.assessment.entity.AssessmentTime;
import com.fosung.frame.modules.assessment.service.AssessmentTimeService;
import com.fosung.frame.modules.partytype.entity.PartyType;

/**
 * 考核期Controller
 * @author wu
 * @version 2017-08-29
 */
@Controller
@RequestMapping(value = "${adminPath}/assessment/assessmentTime")
public class AssessmentTimeController extends BaseController {

	@Autowired
	private AssessmentTimeService assessmentTimeService;
	
	@ModelAttribute
	public AssessmentTime get(@RequestParam(required=false) String id) {
		AssessmentTime entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = assessmentTimeService.get(id);
		}
		if (entity == null){
			entity = new AssessmentTime();
		}
		return entity;
	}
	
	@RequiresPermissions("assessment:assessmentTime:view")
	@RequestMapping(value = {"list", ""})
	public String list(AssessmentTime assessmentTime, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AssessmentTime> page = assessmentTimeService.findPage(new Page<AssessmentTime>(request, response), assessmentTime); 
		model.addAttribute("page", page);
		return "modules/assessment/assessmentTimeList";
	}

	@RequiresPermissions("assessment:assessmentTime:view")
	@RequestMapping(value = "form")
	public String form(AssessmentTime assessmentTime, Model model) {
		model.addAttribute("assessmentTime", assessmentTime);
		return "modules/assessment/assessmentTimeForm";
	}

	@RequiresPermissions("assessment:assessmentTime:edit")
	@RequestMapping(value = "save")
	public String save(AssessmentTime assessmentTime, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, assessmentTime)){
			return form(assessmentTime, model);
		}
		assessmentTimeService.save(assessmentTime);
		addMessage(redirectAttributes, "保存考核期成功");
		return "redirect:"+Global.getAdminPath()+"/assessment/assessmentTime/?repage";
	}
	
	@RequiresPermissions("assessment:assessmentTime:edit")
	@RequestMapping(value = "delete")
	public String delete(AssessmentTime assessmentTime, RedirectAttributes redirectAttributes) {
		assessmentTimeService.delete(assessmentTime);
		addMessage(redirectAttributes, "删除考核期成功");
		return "redirect:"+Global.getAdminPath()+"/assessment/assessmentTime/?repage";
	}
	
	@RequiresPermissions("assessment:assessmentTime:edit")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(AssessmentTime assessmentTime, RedirectAttributes redirectAttributes) {
		String str = assessmentTime.getIds();
		String[] strArr = str.split(",");
		for (int i = 0; i < strArr.length; i++) {
			assessmentTime.setId(strArr[i]);
			assessmentTimeService.delete(assessmentTime);
		}
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/assessment/assessmentTime/?repage";
	}
	@ResponseBody
	@RequiresPermissions("assessment:assessmentTime:edit")
	@RequestMapping(value = "checkCode")
	public String checkCode(String oldyear, String year) {
		AssessmentTime assessmentTime=new AssessmentTime();
		assessmentTime.setYear(year);
		if (year !=null && year.equals(oldyear)) {
			return "true";
		} else if (year !=null && assessmentTimeService.check(assessmentTime) == null) {
			return "true";
		}
		return "false";
	}
}